/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.basicdata.basicpeople.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.basicdata.basicpeople.entity.BasicPeople;
import com.jeeplus.modules.basicdata.basicpeople.mapper.BasicPeopleMapper;
import com.jeeplus.modules.basicdata.basicpeople.entity.BasicPeopleEducation;
import com.jeeplus.modules.basicdata.basicpeople.mapper.BasicPeopleEducationMapper;
import com.jeeplus.modules.basicdata.basicpeople.entity.BasicPeopleJobVote;
import com.jeeplus.modules.basicdata.basicpeople.mapper.BasicPeopleJobVoteMapper;
import com.jeeplus.modules.basicdata.basicpeople.entity.BasicPeoplePositionVote;
import com.jeeplus.modules.basicdata.basicpeople.mapper.BasicPeoplePositionVoteMapper;
import com.jeeplus.modules.basicdata.basicpeople.entity.BasicPeopleUnitAdminVote;
import com.jeeplus.modules.basicdata.basicpeople.mapper.BasicPeopleUnitAdminVoteMapper;

/**
 * 基础人员信息主附表Service
 * @author 刘明星
 * @version 2021-11-18
 */
@Service
@Transactional(readOnly = true)
public class BasicPeopleService extends CrudService<BasicPeopleMapper, BasicPeople> {

	@Autowired
	private BasicPeopleEducationMapper basicPeopleEducationMapper;
	@Autowired
	private BasicPeopleJobVoteMapper basicPeopleJobVoteMapper;
	@Autowired
	private BasicPeoplePositionVoteMapper basicPeoplePositionVoteMapper;
	@Autowired
	private BasicPeopleUnitAdminVoteMapper basicPeopleUnitAdminVoteMapper;

	public BasicPeople get(String id) {
		BasicPeople basicPeople = super.get(id);
		basicPeople.setBasicPeopleEducationList(basicPeopleEducationMapper.findList(new BasicPeopleEducation(basicPeople)));
		basicPeople.setBasicPeopleJobVoteList(basicPeopleJobVoteMapper.findList(new BasicPeopleJobVote(basicPeople)));
		basicPeople.setBasicPeoplePositionVoteList(basicPeoplePositionVoteMapper.findList(new BasicPeoplePositionVote(basicPeople)));
		basicPeople.setBasicPeopleUnitAdminVoteList(basicPeopleUnitAdminVoteMapper.findList(new BasicPeopleUnitAdminVote(basicPeople)));
		return basicPeople;
	}

	public List<BasicPeople> findList(BasicPeople basicPeople) {
		return super.findList(basicPeople);
	}

	public Page<BasicPeople> findPage(Page<BasicPeople> page, BasicPeople basicPeople) {
		return super.findPage(page, basicPeople);
	}

	@Transactional(readOnly = false)
	public void save(BasicPeople basicPeople) {
		super.save(basicPeople);
		for (BasicPeopleEducation basicPeopleEducation : basicPeople.getBasicPeopleEducationList()){
			if (basicPeopleEducation.getId() == null){
				continue;
			}
			if (BasicPeopleEducation.DEL_FLAG_NORMAL.equals(basicPeopleEducation.getDelFlag())){
				if (StringUtils.isBlank(basicPeopleEducation.getId())){
					basicPeopleEducation.setPeople(basicPeople);
					basicPeopleEducation.preInsert();
					basicPeopleEducationMapper.insert(basicPeopleEducation);
				}else{
					basicPeopleEducation.preUpdate();
					basicPeopleEducationMapper.update(basicPeopleEducation);
				}
			}else{
				basicPeopleEducationMapper.delete(basicPeopleEducation);
			}
		}
		for (BasicPeopleJobVote basicPeopleJobVote : basicPeople.getBasicPeopleJobVoteList()){
			if (basicPeopleJobVote.getId() == null){
				continue;
			}
			if (BasicPeopleJobVote.DEL_FLAG_NORMAL.equals(basicPeopleJobVote.getDelFlag())){
				if (StringUtils.isBlank(basicPeopleJobVote.getId())){
					basicPeopleJobVote.setPeople(basicPeople);
					basicPeopleJobVote.preInsert();
					basicPeopleJobVoteMapper.insert(basicPeopleJobVote);
				}else{
					basicPeopleJobVote.preUpdate();
					basicPeopleJobVoteMapper.update(basicPeopleJobVote);
				}
			}else{
				basicPeopleJobVoteMapper.delete(basicPeopleJobVote);
			}
		}
		for (BasicPeoplePositionVote basicPeoplePositionVote : basicPeople.getBasicPeoplePositionVoteList()){
			if (basicPeoplePositionVote.getId() == null){
				continue;
			}
			if (BasicPeoplePositionVote.DEL_FLAG_NORMAL.equals(basicPeoplePositionVote.getDelFlag())){
				if (StringUtils.isBlank(basicPeoplePositionVote.getId())){
					basicPeoplePositionVote.setPeople(basicPeople);
					basicPeoplePositionVote.preInsert();
					basicPeoplePositionVoteMapper.insert(basicPeoplePositionVote);
				}else{
					basicPeoplePositionVote.preUpdate();
					basicPeoplePositionVoteMapper.update(basicPeoplePositionVote);
				}
			}else{
				basicPeoplePositionVoteMapper.delete(basicPeoplePositionVote);
			}
		}
		for (BasicPeopleUnitAdminVote basicPeopleUnitAdminVote : basicPeople.getBasicPeopleUnitAdminVoteList()){
			if (basicPeopleUnitAdminVote.getId() == null){
				continue;
			}
			if (BasicPeopleUnitAdminVote.DEL_FLAG_NORMAL.equals(basicPeopleUnitAdminVote.getDelFlag())){
				if (StringUtils.isBlank(basicPeopleUnitAdminVote.getId())){
					basicPeopleUnitAdminVote.setPeople(basicPeople);
					basicPeopleUnitAdminVote.preInsert();
					basicPeopleUnitAdminVoteMapper.insert(basicPeopleUnitAdminVote);
				}else{
					basicPeopleUnitAdminVote.preUpdate();
					basicPeopleUnitAdminVoteMapper.update(basicPeopleUnitAdminVote);
				}
			}else{
				basicPeopleUnitAdminVoteMapper.delete(basicPeopleUnitAdminVote);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(BasicPeople basicPeople) {
		super.delete(basicPeople);
		basicPeopleEducationMapper.delete(new BasicPeopleEducation(basicPeople));
		basicPeopleJobVoteMapper.delete(new BasicPeopleJobVote(basicPeople));
		basicPeoplePositionVoteMapper.delete(new BasicPeoplePositionVote(basicPeople));
		basicPeopleUnitAdminVoteMapper.delete(new BasicPeopleUnitAdminVote(basicPeople));
	}

}